// pages/test/unlimit/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    timer: null,
    defaultData: {
      staticTop: 600,
      time: 3000,
      middle: 230,
      k: 30
    },
    index: 0,
    boomIndex: [true, true, true, true, true],
    bomb: [],
    boom: { top: 0, left: 0, index: 0 },
    selector: [],
    data: [],
    speedR: 0,
    w: 0,
    life: 5,
    kizu: [],
    point:0,
    firstTap:true,
    tempHistory:[],
  },
  getData(callback) {
    const items = getApp().getItem({
      itemNumber: 10,
      selectNum: 3
    },(res)=>{
      this.setData({
        data:res
      });
      if(callback) callback();
    });
   
  },
  giveUp() {
    clearInterval(this.data.timer);
    getApp().setUserInfo({
      UUID: getApp().data.userInfo.openId,
      expSingle: getApp().data.userDetail.expSingle + this.data.point
    })
    getApp().data.userDetail.expSingle += this.data.point;
    getApp().addHistory(this.data.tempHistory);
    wx.navigateBack({
      delta: 1,
    })
  },
  booom(id) {
    this.data.bomb[id].show = false;
    wx.vibrateShort();
    if (id === this.data.data[this.data.index].right - 1) {
      this.explode(0);
      this.setData({
        point:this.data.point+10
      })
      if(this.data.firstTap){
        this.addHistory(this.data.data[this.data.index].title,1);
      }
      this.data.firstTap=false;
      wx.vibrateLong();
      this.setData({
        bomb: [...this.data.bomb],
        boom: {
          top: this.data.bomb[id].top + 100,
          left: this.data.bomb[id].left + 100,
          index: 0
        }
      })
    }else{
      if(this.data.firstTap){
        this.addHistory(this.data.data[this.data.index].title, 0);
      }
    }
    this.data.firstTap=false;
  },
  onTouch(e) {    
    const id = parseInt(e.currentTarget.id);
    if (this.data.life > 0) {
      this.data.kizu.push({
        left:e.detail.x,
        top:e.detail.y,
        alpha:1,
        dis:true
      })
      const i=this.data.kizu.length-1;
      setTimeout(()=>{this.subAlpha(i)},2000)
      this.setData({
        life: this.data.life - 1,
        kizu: [...this.data.kizu]
      })
      this.booom(id);
    }

  },
  addHistory(title,result,type=0){
    this.data.tempHistory.push({
      UUID:getApp().data.userInfo.openId,
      title:title,
      result:result,
      type:type,
    })
  },
  subAlpha(index){
    if(this.data.kizu[index].alpha<=0){
      this.data.kizu[index].dis=false;
    }else{
      this.data.kizu[index].alpha-=0.1;
      setTimeout(()=>this.subAlpha(index),100);
    }
    this.setData({
      kizu:[...this.data.kizu]
    })
  },
  explode(i) {
    if (i > 4) {
      this.setData({
        boom: { top: 0, left: 0, index: 0 },
        boomIndex: [true, true, true, true, true]
      })
      return;
    }
    this.data.boomIndex = [true, true, true, true, true];

    this.data.boomIndex[i] = false;
    this.setData({
      boomIndex: [...this.data.boomIndex]
    })
    setTimeout(() => this.explode(i + 1), 200);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  init(){
    const that = this;
    const data = this.data;
    data.bomb = [];
    for (let i = 0; i < 3; i++) {
      const ranLeft = i * 300 - 70;
      const ranTop = Math.random() * 100;
      data.bomb.push({
        left: ranLeft,
        toLeft: data.defaultData.middle - ranLeft,
        top: data.defaultData.staticTop,
        toTop: ranTop - data.defaultData.staticTop,
        rotate: 360 * Math.random(),
        speedX: (data.defaultData.middle - ranLeft) / data.defaultData.time * data.defaultData.k,
        speedY: 0,
        w: 0,
        text: data.data[data.index].selector[i],
        key: i,
        show: true
      })
    }
    data.timer = setInterval(() => {
      data.w = data.w + 100 / data.defaultData.time;
      data.speedR = Math.abs(Math.cos(data.w));

      for (const item of data.bomb) {
        // if (!item.show) continue;
        item.rotate += data.speedR * 10;

        item.left += item.speedX;

        if (item.left < -40) {
          item.speedX = Math.abs(item.speedX);
        }
        if (item.left > 500) {
          item.speedX = -Math.abs(item.speedX);
        }
        item.w += 100 / data.defaultData.time;

        item.speedY = -(Math.cos(item.w)) * 50000 / data.defaultData.time;
        item.top += item.speedY;
        for (const item2 of data.bomb) {
          if (item2 === item || !item2.show) continue;
          if (Math.sqrt(Math.pow(item2.left - item.left, 2) + Math.pow(item2.top - item.top, 2)) < 190) {
            const temp = item2.speedX;
            item2.speedX = item.speedX;
            item.speedX = temp;
          }
        }
      }

      if (data.bomb[data.data[data.index].right - 1].top > 600 && data.bomb[data.data[data.index].right - 1].show) {
        //game over
        this.booom(data.data[data.index].right - 1);
        setTimeout(() => {
          this.giveUp();
        }, 500)
      }
      if (data.bomb[0].top > 800) {
        //下一题
        if (data.index < data.data.length - 1) {
          data.index++;
          that.setData({
            index: data.index,
            firstTap:true
          })
        } else {
          that.getData();
          that.setData({
            index: 0
          });
        }
        data.bomb = [];
        for (let i = 0; i < 3; i++) {
          const ranLeft = i * 300 - 70;
          const ranTop = Math.random() * 100;
          data.bomb.push({
            left: ranLeft,
            toLeft: data.defaultData.middle - ranLeft,
            top: data.defaultData.staticTop,
            toTop: ranTop - data.defaultData.staticTop,
            rotate: 360 * Math.random(),
            speedX: (data.defaultData.middle - ranLeft) / data.defaultData.time * data.defaultData.k,
            speedY: 0,
            w: 0,
            text: data.data[data.index].selector[i],
            key: i,
            show: true
          })

        }
        that.setData({
          life: data.life + 1,
          bomb: [...data.bomb],
          selector: [...data.data[this.data.index].selector]
        })


        data.w = 0;
      }
      that.setData({

        bomb: [...data.bomb],

      })
    }, 50);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getData(this.init);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    clearInterval(this.data.timer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    clearInterval(this.data.timer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})